/**
 * Created by gongxun on 16/10/28.
 */

var FileHelper = function(){
    var that = {};

    that.setItem = function(key, value){
        cc.sys.localStorage.setItem(key, value);
    };

    that.getItem = function(key){
        return cc.sys.localStorage.getItem(key);
    };

    that.removeItem = function(key){
        cc.sys.localStorage.removeItem(key);
    };

    that.removeAllItem = function(){
        cc.sys.localStorage.clear();
    };

    that.getLoginInfo = function(){
        var uid = that.getItem("logininfo");
        return uid;
    };

    that.setLoginInfo = function(uid){
        that.setItem("logininfo", uid);
        that.setHardLoginInfo(uid);
    };

    that.getHardLoginInfo = function(){
        var keyCode = that.getItem("keycode");
        if (keyCode == undefined){
            return null;
        }else{
            var pwd = "asdhaldjlajksdlakljsdkjlajlksdjlaksdjlkalkjsda";
            var miBase64 = Base64.decode(keyCode);
            var result = CryptoJS.AES.decrypt(miBase64, pwd).toString(CryptoJS.enc.Utf8);
            return result;
        }
    };

    that.setHardLoginInfo = function(test){
        var pwd = "asdhaldjlajksdlakljsdkjlajlksdjlaksdjlkalkjsda";
        var mi = CryptoJS.AES.encrypt(test, pwd);
        var miBase64 = Base64.encode(mi);
        that.setItem("keycode", miBase64);
    };

    //本地的remark
    that.getHardServerInfo = function () {
        var keyCode = that.getItem("server_remark");
        if (keyCode == undefined){
            return null;
        }else{
            var pwd = "asdhaldjlajksdlakljsdkjlajlksdjlaksdjlkalkjsda";
            var miBase64 = Base64.decode(keyCode);
            var result = CryptoJS.AES.decrypt(miBase64, pwd).toString(CryptoJS.enc.Utf8);
            return result;
        }
    };

    that.setHardServerInfo = function (remark) {
        var pwd = "asdhaldjlajksdlakljsdkjlajlksdjlaksdjlkalkjsda";
        var mi = CryptoJS.AES.encrypt(remark, pwd);
        var miBase64 = Base64.encode(mi);
        that.setItem("server_remark", miBase64);
    };

    //上次登录成功的ip
    that.getLastLoginInfo = function(){
        var info = that.getItem("key_last_login");
        cc.log("读取到本地logininfo1:" + info);
        if (info == undefined){
            return null;
        }else{
            var pwd = "asdhaldjlajksdlakljsdkjlajlksdjlaksdjlkalkjsda";
            var miBase64 = Base64.decode(info);
            var result = CryptoJS.AES.decrypt(miBase64, pwd).toString(CryptoJS.enc.Utf8);
            return result;
        }
    };

    that.setLastLoginInfo = function(info){
        var pwd = "asdhaldjlajksdlakljsdkjlajlksdjlaksdjlkalkjsda";
        var mi = CryptoJS.AES.encrypt(info, pwd);
        var miBase64 = Base64.encode(mi);
        that.setItem("key_last_login", miBase64);
    };

    //设置本地断线重连超时 让下次手动登录
    that.setAccessTokenAndLoginNameTimeOut = function(){
        var timeKey = "reconReconneceTime";
        that.setItem(timeKey, mo.gameHelper.getTimestamp() - 43200);
    };

    //保存断线重连的数据 2个小时 登录成功之后刷新
    that.setAccessTokenAndLoginName = function(accesstoken, loginName){
        //保存断线重连的数据 time accesstoken loginname
        var timeKey = "reconReconneceTime";
        var accesstokenKey = "reconReconneceAccessstoken";
        var loginNameKey = "reconReconneceLoginName";

        //秒
        var timestamp = mo.gameHelper.getTimestamp();

        that.setItem(timeKey, timestamp);
        that.setItem(accesstokenKey, accesstoken);
        that.setItem(loginNameKey, loginName);
    };

    //读取断线重连的数据
    that.getAccessTokenAndLoginName = function(func){
        var timeKey = "reconReconneceTime";
        var accesstokenKey = "reconReconneceAccessstoken";
        var loginNameKey = "reconReconneceLoginName";

        var timeLocal = that.getItem(timeKey);
        //是否过期
        var isExpired = true;
        if (timeLocal != undefined){
            var jiange = mo.gameHelper.getTimestamp() - timeLocal;
            cc.log("时间戳间隔:" + jiange + "过期间隔:43200");
            isExpired = jiange > 43200;
        }

        func(isExpired, that.getItem(accesstokenKey), that.getItem(loginNameKey));
    };

    //读取manifest文件
    that.getManifestUrl = function(){
        var key = "key_manifestUrl";
        var result = that.getItem(key);
        if (result == undefined){
            return "";
        }
        return result;
    };

    //写manifest文件
    that.setManifestUrl = function(url){
        var key = "key_manifestUrl";
        that.setItem(key, url);
    };

    return that;
};

mo.fileHelper = new FileHelper();
